package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result add(@RequestBody Permission permission){
        log.debug("controller{"+permission+"}");
        permissionService.add(permission);
        log.debug("权限数据添加成功！！");
        return new Result(true, MessageConst.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 分页查询
     *  1.调用Service获取分页结果数据
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller{"+queryPageBean+"}");
        PageResult pageResult = permissionService.findPage(queryPageBean);
        log.debug("权限数据分页查询成功!!");
        return new Result(true, MessageConst.QUERY_PERMISSION_SUCCESS, pageResult);
    }

    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result delById(Integer id){
        log.debug("PermissionController:delete: " + id);
        permissionService.delById(id);
        log.debug("权限数据删除成功!!");
        return new Result(true, MessageConst.DELETE_PERMISSION_SUCCESS);
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result findById(Integer id){
        log.debug("PermissionController：findById:" + id);
        Permission permission = permissionService.findById(id);
        log.debug("根据id查询权限数据成功!!");
        return new Result(true, MessageConst.QUERY_PERMISSION_SUCCESS, permission);
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result edit(@RequestBody Permission permission){
        log.debug("PermissionController:edit: " + permission);
        permissionService.edit(permission);
        log.debug("权限数据修改成功!!");
        return new Result(true, MessageConst.EDIT_PERMISSION_SUCCESS);
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result findAll(){
        log.debug("findAll");
        List<Permission> permissionList = permissionService.findAll();
        log.debug("permissionList:" + permissionList);
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS, permissionList);
    }
}
